package xm.control.bar
{
	import xm.control.bar.barContent.ActionContent;
	import xm.control.bar.barContent.NavigationContent;
	import xm.control.bar.barContent.TitleContent;
	import xm.control.bar.base.BarBase;
	import xm.control.bar.base.ContentBase;
	
	/**
	 * <br> 클래스 설명 : </br>
	 * @author 김정환
	 */	
	public class ActionBar extends BarBase
	{
		
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		
		/**
		 * NavigationContent
		 */		
		protected var navigationContent : NavigationContent;
		
		/**
		 * TitleContent
		 */		
		protected var titleContent : TitleContent;
		
		/**
		 * ActionContent
		 */		
		protected var actionContent : ActionContent;
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * constructor 
		 */	
		public function ActionBar()
		{
			super();
		}
		
		
		/**
		 * @param content : ContentBase : NavigationContent
		 */		
		public function setNavigationContent( content : NavigationContent ):void
		{
			if( navigationContent )
			{
				navigationContent.remove();
			}
			
			navigationContent = content;
			setContent( content as ContentBase );
			
			this.invalidateDisplayList();
		}
		
		
		/**
		 * @param content : ContentBase : TitleContent
		 */		
		public function setTitleContent( content : TitleContent ):void
		{
			if( titleContent )
			{
				titleContent.remove();
			}
			
			titleContent = content;
			setContent( content as ContentBase );
			
			this.invalidateDisplayList();
		}
		
		
		/**
		 * @param content : ContentBase : ActionContent
		 */		
		public function setActionContent( content : ActionContent ):void
		{
			if( actionContent )
			{
				actionContent.remove();
			}
			
			actionContent = content;
			setContent( content as ContentBase );
			
			this.invalidateDisplayList();
		}
		

		
		/**
		 *  updateDisplayList
		 */		
		override public function updateDisplayList( enforce : Boolean = false ):void
		{
			super.updateDisplayList( enforce );
			
			if( navigationContent ) navigationContent.apply( this );
			if( actionContent ) actionContent.apply( this );
			if( titleContent ) titleContent.apply( this );
			
		}
		
		
		/**
		 * @return : Number - 사용가능한 titleContent의 너비.
		 */		
		public function availableTitleContentWidth():Number
		{
			return s.stageWidth - paddingLeft - paddingRight 
				- ( ( this.isNavigationContent ) ? navigationContentWidth : 0 )
				- ( ( this.isActionContent ) ? actionContentWidth : 0 )
				- ( ( this.titleContent ) ? this.titleContent.paddingLeft - this.titleContent.paddingRight : 0 );
		}

		
		
		/**
		 * destroy
		 */		
		override public function destroy():void
		{
			
			if( navigationContent )
			{
				navigationContent.destroy();
				navigationContent = null;
			}
			
			if( titleContent )
			{
				titleContent.destroy();
				titleContent = null;
			}
			
			if( actionContent )
			{
				actionContent.destroy();
				actionContent = null;
			}
			
			super.destroy();
		}
		
		
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		
		/**
		 * @return Boolean
		 */		
		public function get isNavigationContent():Boolean
		{
			return ( navigationContent && navigationContent.isAdded ) ? true : false; 
		}
		
		
		/**
		 * @return Boolean
		 */		
		public function get isTitleContent():Boolean
		{
			return ( titleContent && titleContent.isAdded ) ? true : false;
		}
		
		/**
		 * @return Boolean
		 */		
		public function get isActionContent():Boolean
		{
			return ( actionContent && actionContent.isAdded ) ? true : false;
		}
		
		
		/**
		 * 네비게이션 컨텐츠의 너비
		 * <br /> 
		 * @return 
		 * 
		 */		
		public function get navigationContentWidth():Number
		{
			return (navigationContent)?navigationContent.width:0;
		}
		
		/**
		 * 타이틀 커텐츠의 너비
		 * <br /> 
		 * @return 
		 * 
		 */		
		public function get titleContentWidth():Number
		{
			return (titleContent)?titleContent.width:0;
		}
		
		/**
		 * 액션 컨텐츠의 너비.
		 * <br />
		 * @return 
		 */		
		public function get actionContentWidth():Number
		{
			return (actionContent)?actionContent.width:0;
		}
		
		
		//-----------------------------------------------------------------------------------
		// protected functions
		//-----------------------------------------------------------------------------------
		
		
		//-----------------------------------------------------------------------------------
		// internal functions
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		
		// 컨텐츠 공통 기본셋팅 
		private function setContent( content : ContentBase ):void
		{
			if( content != null )
			{
				content.apply( this );
				content.createChildren();
				content.add();
			}
			
			if( navigationContent == null && titleContent == null && actionContent == null ) 
			{
				this.removeFromStage();
			}
			else if( s.contains( this ) == false )
			{
				this.addToStage();
			}
			
		}
		
		
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		
		
		
		
		
		
		
		
		
		
	}
}